# coding=utf8
"""
更新集成计划版本变化记录
1、修改change_record表is_after_hand字段
"""
import requests
from app import create_app
from app.modules.common.utils import DbCommit, recursive_json_loads
from app.modules.integration.models import IntegrationEditionChengeRecord

application = create_app(flask_config = 'local')


def save():
    with application.app_context():
        # 获取用户信息列表
        _url = 'http://192.168.56.113:5001/api/v1/user/?token_id=9d69856241b911e79cf80800270930fa'
        _obj = recursive_json_loads(requests.get(url = _url).content)
        if not _obj['success']:
            print "找不到用户啦%s" % _obj
            return
        _users = _obj['data']
        for c in IntegrationEditionChengeRecord.query.all():
            if c.integration_status != "HAND_OVER" and c.integration_status != "ONLINE":
                c.is_after_hand = "1"
            for user in _users:
                if str(user.id) == str(c.user_id):
                    _role = None
                    for ro in user['role_list']:
                        if ro.role_name in ['dev', 'qa', 'ops']:
                            _role = ro.role_name
                            break
                    if _role is None:
                        for ro in user['role_list']:
                            if ro.role_name == 'admin':
                                _role = ro.role_name
                                break
                    c.user_role = _role
                    break
        DbCommit()
        pass


if __name__ == '__main__':
    save()
    pass
